package com.kapi.kapigateway;


import com.kapi.project.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.io.File;

/**
 * 网关入口
 * @author kjwka
 */
@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class}
        )
@EnableDubbo(scanBasePackages = {"com.kapi.project"})
@Slf4j
public class KapiGatewayApplication {

    @DubboReference
    private DemoService demoService;

    public static void main(String[] args) {

        // 解决Dubbo本地启动多个应用的缓存冲突问题
        SpringApplication application = new SpringApplication(KapiGatewayApplication.class);
        // 1.添加自定义的ApplicationContextInitializer
        application.addInitializers(context -> {
            // 2.获取Environment对象
            Environment env = context.getEnvironment();
            // 3.从Environment中读取"spring.application.name"属性值
            String appName = env.getProperty("spring.application.name");
            String filePath = System.getProperty("user.home") + File.separator + ".dubbo" + File.separator + appName;
            // 4.修改dubbo的本地缓存路径，避免缓存冲突
            System.setProperty("dubbo.meta.cache.filePath", filePath);
            System.setProperty("dubbo.mapping.cache.filePath", filePath);
        });
        // 5.启动应用
        application.run(args);
        log.info("api-gateway启动成功...");
    }

}
